package com.huazai.out_school_manager.Service.realization;

import com.huazai.out_school_manager.Dao.realization.UserDaoReal;
import com.huazai.out_school_manager.Service.UserService;

import javax.servlet.http.HttpServletRequest;

public class UserServiceReal implements UserService {
    private final UserDaoReal userDao = new UserDaoReal();

    @Override
    public String doRegister(String username, String password, String userid, String userField, HttpServletRequest request) {
        String mes = null;

        if ( userDao.checkUsername(userid, userField) ) {
            mes = "该用ID已经注册过了。。。";
            return mes;
        }
        else if ( userDao.signup(username, userid, password, userField) ) {
            mes = "注册成功，快回去登录吧 ~~~";
        }
        else {
            mes = "注册失败了。。。请重试 ";
        }
        return mes;
    }

    @Override
    public String doLogin(String userid, String password, String userField, HttpServletRequest request) {
        String mes;

        if (!userDao.checkUsername(userid, userField)) {
            mes = "登录失败，用户不存在";
            return mes;
        }
        if ( userDao.login(userid, password, userField) ) {
            request.setAttribute("userid",userid);
            mes = "登录成功";
            return mes;
        }

        mes = "登录失败，密码错误";
        return mes;
    }
}
